

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>cogentviewer.models.reading &mdash; Cogent Viewer 0.1 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/rtd.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Cogent Viewer 0.1 documentation" href="../../../index.html" />
    <link rel="up" title="Module code" href="../../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">Cogent Viewer 0.1 documentation</a> &raquo;</li>
          <li><a href="../../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for cogentviewer.models.reading</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">.. codeauthor::  Ross Wiklins </span>
<span class="sd">.. codeauthor::  James Brusey</span>
<span class="sd">.. codeauthor::  Daniel Goldsmith &lt;djgoldsmith@googlemail.com&gt;</span>
<span class="sd">&quot;&quot;&quot;</span>


<span class="kn">import</span> <span class="nn">sqlalchemy</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">meta</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">Base</span>


<span class="c"># from sqlalchemy.ext.declarative import declarative_base</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">Table</span><span class="p">,</span> <span class="n">Column</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">,</span> <span class="n">DateTime</span><span class="p">,</span> <span class="n">Float</span><span class="p">,</span> <span class="n">Boolean</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">relationship</span><span class="p">,</span> <span class="n">backref</span>

<span class="kn">import</span> <span class="nn">time</span>

<span class="c">#To allow Calibration</span>
<span class="kn">import</span> <span class="nn">sensor</span>
<span class="kn">import</span> <span class="nn">dateutil</span>

<div class="viewcode-block" id="Reading"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.reading.Reading">[docs]</a><span class="k">class</span> <span class="nc">Reading</span><span class="p">(</span><span class="n">Base</span><span class="p">,</span><span class="n">meta</span><span class="o">.</span><span class="n">InnoDBMix</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Table to hold detils of readings,</span>

<span class="sd">    Reading has a composite primary key consisiting of time,nodeId,typeId</span>

<span class="sd">    :var DateTime time: Time reading was taken</span>
<span class="sd">    :var Integer nodeId: Id of :class:`cogentviewer.models.node.Node` that took this sample</span>
<span class="sd">    :var Integer typeId: Id of :class:`cogentviwer.models.sensortype.SensorType` that this reading came from</span>
<span class="sd">    :var Integer locationId: Id of :class:`cogentviewer.models.location.Location` that this reading came from</span>

<span class="sd">    :var float value: The sensor reading itself</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&quot;Reading&quot;</span>


    <span class="n">time</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">,</span>
                  <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                  <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                  <span class="n">autoincrement</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
    <span class="n">nodeId</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span>
                    <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;Node.id&#39;</span><span class="p">),</span>
                    <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                    <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                    <span class="n">autoincrement</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="n">typeId</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> 
                    <span class="n">Integer</span><span class="p">,</span> 
                    <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;SensorType.id&#39;</span><span class="p">),</span>
                    <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                    <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                    <span class="n">autoincrement</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="n">locationId</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span>
                        <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;Location.id&#39;</span><span class="p">),</span>
                        <span class="n">autoincrement</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="n">value</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Float</span><span class="p">)</span>    

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&quot;Reading(&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">time</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;,&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodeId</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;,&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">typeId</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;,&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;)&quot;</span>
        
    
    <span class="c">#def __eq__(self,other):</span>
    <span class="c">#    return self.value == other.value</span>
    
    <span class="k">def</span> <span class="nf">__cmp__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">other</span><span class="p">):</span>
        <span class="c">#log.debug(&quot;Self {0}&quot;.format(self))</span>
        <span class="c">#log.debug(&quot;Other {0}&quot;.format(other))</span>
                  
        <span class="n">diffs</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">time</span> <span class="o">-</span> <span class="n">other</span><span class="o">.</span><span class="n">time</span><span class="p">)</span><span class="o">.</span><span class="n">seconds</span>
        <span class="n">diffs</span> <span class="o">+=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodeId</span> <span class="o">-</span> <span class="n">other</span><span class="o">.</span><span class="n">nodeId</span><span class="p">)</span>
        <span class="n">diffs</span> <span class="o">+=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">typeId</span> <span class="o">-</span> <span class="n">other</span><span class="o">.</span><span class="n">typeId</span><span class="p">)</span>
        <span class="n">diffs</span> <span class="o">+=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">locationId</span> <span class="o">-</span> <span class="n">other</span><span class="o">.</span><span class="n">locationId</span><span class="p">)</span>
        <span class="n">diffs</span> <span class="o">+=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">-</span> <span class="n">other</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="mi">0</span>
        
<div class="viewcode-block" id="Reading.asJSON"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.reading.Reading.asJSON">[docs]</a>    <span class="k">def</span> <span class="nf">asJSON</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">slope</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">offset</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return this reading as a JSON relevant tuple</span>
<span class="sd">        </span>
<span class="sd">        This should format the sample so it is parseable by JSON / Javascript</span>
<span class="sd">        That will allow it to be graphed using either DOJO or Highcharts</span>

<span class="sd">        The format is (time,value) where time is Unixtime in miliseconds</span>
<span class="sd">        </span>
<span class="sd">        .. warning::</span>
<span class="sd">        </span>
<span class="sd">            The original timestamp is multipled by 1000, to shift from seconds to miliseconds.</span>
<span class="sd">            As per the JSON / Javascript Standard.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">time</span><span class="o">.</span><span class="n">timetuple</span><span class="p">())</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">,(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="o">*</span><span class="n">slope</span><span class="p">)</span><span class="o">+</span><span class="n">offset</span><span class="p">)</span>
        
</div>
<div class="viewcode-block" id="Reading.getRawValues"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.reading.Reading.getRawValues">[docs]</a>    <span class="k">def</span> <span class="nf">getRawValues</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the Reading as a Tuple of (time,value)</span>
<span class="sd">        </span>
<span class="sd">        Unlike other methods this does no data processing, returning the Raw (rather than calibrated)</span>
<span class="sd">        sensor reading.</span>

<span class="sd">        :return DateTime time: Time that this reading was taken</span>
<span class="sd">        :return Float value: Value of the reading at this Time</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">time</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="Reading.getCalibValues"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.reading.Reading.getCalibValues">[docs]</a>    <span class="k">def</span> <span class="nf">getCalibValues</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the Reading as a tuple of (time,calibratedValue)</span>
<span class="sd">        </span>
<span class="sd">        This returns the same as GetRawValues but will calibrate the data against stored values</span>
<span class="sd">        </span>
<span class="sd">        .. warning:</span>
<span class="sd">        </span>
<span class="sd">            This will issue an SQL statement for each reading we call the function on,</span>
<span class="sd">            Therefore it is only useful as a shortcut when dealing with a limited amount of readings</span>
<span class="sd">            </span>
<span class="sd">        </span>
<span class="sd">        </span>
<span class="sd">        :return DateTime time: Time that this reading was taken</span>
<span class="sd">        :return Float value: Value of the reading at this Time Calibrated against the sensor values</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c">#pass</span>
        <span class="c">#Find the Sensor</span>
        <span class="n">session</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
        <span class="n">theSensor</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">sensor</span><span class="o">.</span><span class="n">Sensor</span><span class="p">)</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">sensorTypeId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">typeId</span><span class="p">,</span>
                                                           <span class="n">nodeId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodeId</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
        <span class="c">#print theSensor</span>
        
        <span class="n">value</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">*</span> <span class="n">theSensor</span><span class="o">.</span><span class="n">calibrationSlope</span><span class="p">)</span> <span class="o">+</span> <span class="n">theSensor</span><span class="o">.</span><span class="n">calibrationOffset</span>

        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">time</span><span class="p">,</span><span class="n">value</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="Reading.toDict"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.reading.Reading.toDict">[docs]</a>    <span class="k">def</span> <span class="nf">toDict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Helper Method to convert an object to a dictionary.</span>

<span class="sd">        This will return a dictionary object, representing this object.  As the</span>
<span class="sd">        Reading table does some trickery with reading.typeId remapping it to</span>
<span class="sd">        (type) then we need to take account of that.</span>

<span class="sd">        .. note::  As this is intended to simplify conversion to and from JSON,</span>
<span class="sd">                   datetimes are converted using .isoformat()</span>

<span class="sd">        :return:: A dictionary of {__table__:&lt;tablename&gt; .. (key,value)* pairs}</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c">#Appending a table to the dictionary could help us be a little cleverer</span>
        <span class="c">#when unpacking objects</span>
        <span class="n">out</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;__table__&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__tablename__</span><span class="p">}</span>

        <span class="c">#Iterate through each column in our table</span>
        <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">&quot;type&quot;</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="s">&quot;typeId&quot;</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

            <span class="c">#Conversion code for datetime</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">sqlalchemy</span><span class="o">.</span><span class="n">DateTime</span><span class="p">)</span> <span class="ow">and</span> <span class="n">value</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
            <span class="c">#Append to the dictionary</span>
            <span class="n">out</span><span class="p">[</span><span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

        <span class="k">return</span> <span class="n">out</span>
</div>
<div class="viewcode-block" id="Reading.fromJSON"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.reading.Reading.fromJSON">[docs]</a>    <span class="k">def</span> <span class="nf">fromJSON</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">jsonDict</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Update the object using a JSON string</span>

<span class="sd">        :var jsonDict:: Either a JSON string (from json.dumps) or dictionary</span>
<span class="sd">        containing key,value pairs (from asDict())</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c">#Check if we have a string or dictonary</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">jsonDict</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
            <span class="n">jsonDict</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">jsonDict</span><span class="p">)</span>

        <span class="c">#For each column in the table</span>
        <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
            <span class="c">#Check to see if the item exists in our dictionary</span>
            <span class="n">newValue</span> <span class="o">=</span> <span class="n">jsonDict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>

            <span class="c">#Fix missing values</span>
            <span class="k">if</span> <span class="n">newValue</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c">#Convert if it is a datetime object</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">col</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">sqlalchemy</span><span class="o">.</span><span class="n">DateTime</span><span class="p">)</span> <span class="ow">and</span> <span class="n">newValue</span><span class="p">:</span>
                    <span class="n">newValue</span> <span class="o">=</span> <span class="n">dateutil</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">newValue</span><span class="p">)</span>

                <span class="c">#And set our variable</span>

                <span class="c">#And Deal with the corner case above</span>
                <span class="k">if</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">&quot;type&quot;</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&quot;typeId&quot;</span><span class="p">,</span> <span class="n">newValue</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">col</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">newValue</span><span class="p">)</span>


</div></div>
<div class="viewcode-block" id="calibrateReadings"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.reading.calibrateReadings">[docs]</a><span class="k">def</span> <span class="nf">calibrateReadings</span><span class="p">(</span><span class="n">theQuery</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generator object to calibate all readings,</span>
<span class="sd">    hopefully this gathers all calibration based readings into one </span>
<span class="sd">    area</span>

<span class="sd">    :param theQuery: SQLA query object containing Reading values&quot;&quot;&quot;</span>

    <span class="c">#Dictionary to hold all sensor paramters</span>
    <span class="n">session</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
    <span class="n">sensorParams</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">for</span> <span class="n">reading</span> <span class="ow">in</span> <span class="n">theQuery</span><span class="p">:</span>
        <span class="c">#log.debug(&quot;Original Reading {0}&quot;.format(reading))</span>
        <span class="n">theSensor</span> <span class="o">=</span> <span class="n">sensorParams</span><span class="o">.</span><span class="n">get</span><span class="p">((</span><span class="n">reading</span><span class="o">.</span><span class="n">nodeId</span><span class="p">,</span><span class="n">reading</span><span class="o">.</span><span class="n">typeId</span><span class="p">),</span><span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">theSensor</span><span class="p">:</span>
            <span class="n">theSensor</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">sensor</span><span class="o">.</span><span class="n">Sensor</span><span class="p">)</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">nodeId</span> <span class="o">=</span> <span class="n">reading</span><span class="o">.</span><span class="n">nodeId</span><span class="p">,</span><span class="n">sensorTypeId</span> <span class="o">=</span> <span class="n">reading</span><span class="o">.</span><span class="n">typeId</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;--&gt; Fetching Sensor {0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">theSensor</span><span class="p">))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">theSensor</span><span class="p">:</span>
                <span class="n">theSensor</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Sensor</span><span class="p">(</span><span class="n">calibrationSlope</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">calibrationOffset</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
                <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;--&gt; Using Default Sensor&quot;</span><span class="p">)</span>
            <span class="c">#print &quot;USING SENSOR {0}&quot;.format(theSensor)</span>
            <span class="n">sensorParams</span><span class="p">[(</span><span class="n">reading</span><span class="o">.</span><span class="n">nodeId</span><span class="p">,</span><span class="n">reading</span><span class="o">.</span><span class="n">typeId</span><span class="p">)]</span> <span class="o">=</span> <span class="n">theSensor</span>

        <span class="n">cReading</span> <span class="o">=</span> <span class="n">Reading</span><span class="p">(</span><span class="n">time</span> <span class="o">=</span> <span class="n">reading</span><span class="o">.</span><span class="n">time</span><span class="p">,</span>
                           <span class="n">nodeId</span> <span class="o">=</span> <span class="n">reading</span><span class="o">.</span><span class="n">nodeId</span><span class="p">,</span>
                           <span class="n">typeId</span> <span class="o">=</span> <span class="n">reading</span><span class="o">.</span><span class="n">typeId</span><span class="p">,</span>
                           <span class="n">locationId</span> <span class="o">=</span> <span class="n">reading</span><span class="o">.</span><span class="n">locationId</span><span class="p">,</span>
                           <span class="n">value</span> <span class="o">=</span> <span class="n">theSensor</span><span class="o">.</span><span class="n">calibrationOffset</span> <span class="o">+</span> <span class="p">(</span><span class="n">theSensor</span><span class="o">.</span><span class="n">calibrationSlope</span> <span class="o">*</span> <span class="n">reading</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                           <span class="c">#value = 10#theSensor.calibrationOffset + (theSensor.calibrationSlope * reading.value))</span>
                           <span class="p">)</span>

        <span class="k">yield</span> <span class="n">cReading</span>
                                  
</div>
<div class="viewcode-block" id="calibrateJSON"><a class="viewcode-back" href="../../../models.html#cogentviewer.models.reading.calibrateJSON">[docs]</a><span class="k">def</span> <span class="nf">calibrateJSON</span><span class="p">(</span><span class="n">theQuery</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generator object to calibrate readings and return in JSON </span>
<span class="sd">    friendly format</span>
<span class="sd">    </span>
<span class="sd">    :param theQuery: SQLA query object containing readings</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c">#Dictionary to hold all sensor paramters</span>
    <span class="n">session</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
    <span class="n">sensorParams</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">for</span> <span class="n">reading</span> <span class="ow">in</span> <span class="n">theQuery</span><span class="p">:</span>
        <span class="c">#log.debug(&quot;Original Reading {0}&quot;.format(reading))</span>
        <span class="n">theSensor</span> <span class="o">=</span> <span class="n">sensorParams</span><span class="o">.</span><span class="n">get</span><span class="p">((</span><span class="n">reading</span><span class="o">.</span><span class="n">nodeId</span><span class="p">,</span><span class="n">reading</span><span class="o">.</span><span class="n">typeId</span><span class="p">),</span><span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">theSensor</span><span class="p">:</span>
            <span class="n">theSensor</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">sensor</span><span class="o">.</span><span class="n">Sensor</span><span class="p">)</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">nodeId</span> <span class="o">=</span> <span class="n">reading</span><span class="o">.</span><span class="n">nodeId</span><span class="p">,</span><span class="n">sensorTypeId</span> <span class="o">=</span> <span class="n">reading</span><span class="o">.</span><span class="n">typeId</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
            <span class="c">#log.debug(&quot;--&gt; Fetching Sensor {0}&quot;.format(theSensor))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">theSensor</span><span class="p">:</span>
                <span class="n">theSensor</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Sensor</span><span class="p">(</span><span class="n">calibrationSlope</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">calibrationOffset</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
                <span class="c">#log.debug(&quot;--&gt; Using Default Sensor&quot;)</span>
                
            <span class="n">sensorParams</span><span class="p">[(</span><span class="n">reading</span><span class="o">.</span><span class="n">nodeId</span><span class="p">,</span><span class="n">reading</span><span class="o">.</span><span class="n">typeId</span><span class="p">)]</span> <span class="o">=</span> <span class="n">theSensor</span>

        <span class="n">sVal</span> <span class="o">=</span> <span class="n">theSensor</span><span class="o">.</span><span class="n">calibrationOffset</span> <span class="o">+</span> <span class="p">(</span><span class="n">theSensor</span><span class="o">.</span><span class="n">calibrationSlope</span> <span class="o">*</span> <span class="n">reading</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                               
        <span class="k">yield</span> <span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">reading</span><span class="o">.</span><span class="n">time</span><span class="o">.</span><span class="n">timetuple</span><span class="p">())</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">,</span><span class="n">sVal</span><span class="p">)</span>
    </div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">Cogent Viewer 0.1 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2011, Daniel Goldsmith.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>